﻿using System;
using System.Collections.Generic;
using System.Collections.ObjectModel;
using System.Linq;
using System.Text;
using System.ComponentModel;


using System.Xml;
using System.Xml.Serialization;
using System.IO;

namespace HighDivePrinter.Model
{
    /// <summary>
    /// List holding dives, used for queue and history).
    /// </summary>
    [Serializable()]
    public class DiveScoreList : IDiveScoreList
    {
        private BindingList<DiveScore> _listScore;

        #region Properties
        public BindingList<DiveScore> ListScore
        {
            get { return _listScore; }
            set { _listScore = value; }
        }
        #endregion


         #region Constructors
        public DiveScoreList()
        {
            ListScore = new BindingList<DiveScore>();
        }

        #endregion

      /*  public double? Sum()
        {
            return this.Sum(x => x.Score);
        }

        public double? Max()
        {
            return this.Max(x => x.Score);
        }

        public double? Min()
        {
            return this.Min(x => x.Score);
        }
        */
        #region IDiveScoreList Members

        void IDiveScoreList.AddScore(DiveScore d)
        {
            _listScore.Add(d);
        }

        void IDiveScoreList.RemoveScore(int index)
        {
            _listScore.RemoveAt(index);
        }

        void IDiveScoreList.SaveScore()
        {
            Stream stream = new FileStream("Contests.xml", FileMode.Create, FileAccess.Write, FileShare.Write);
            XmlSerializer xs = new XmlSerializer(typeof(DiveScoreList));
            xs.Serialize(stream, this);
            stream.Close();
        } 

        void IDiveScoreList.ReadScore()
        {
            Stream stream = new FileStream("Contests.xml", FileMode.Open, FileAccess.Read, FileShare.Read);
            XmlSerializer xs = new XmlSerializer(typeof(DiveScoreList));
            DiveScoreList dt = (DiveScoreList)xs.Deserialize(stream);

            _listScore.Clear();
            foreach (var c in dt.ListScore)
            {
                _listScore.Add(c);
            }
            stream.Close();
        }

        #endregion
    }
}
